#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cstring>

using namespace std;

int main()
{
    const char *filename = "./connFifo";
    int ret = access(filename, F_OK);
    if (ret != 0)
    {
        // 创建文件
        ret = mkfifo(filename, 0644);
        if (ret != 0)
        {
            perror("mkfifo error");
            exit(-1);
        }
    }

    // 1、打开文件
    int fd = open(filename, O_RDWR);
    if (fd == -1)
    {
        perror("open error");
        _exit(-1);
    }

    // 2、读取数据
    char buffer[11] = { 0 };
    int readBytes = 0;
    while (1)
    {
        readBytes = read(fd, buffer, sizeof(buffer));
        if (readBytes < 0)
        {
            perror("read error");
            _exit(-1);
        }
        cout << "reafBytes:" << readBytes << ", buffer:" << buffer << endl;
    }

    // 3、关闭文件
    close(fd);
    
    return 0;
}